package com.scbo.activity;

import java.util.ArrayList;

import com.scbo.DTO.Buildorder;
import com.scbo.DTO.ItemBO;
import com.scbo.database.ScboDAO;
import com.scbo.main.R;
import com.scbo.utility.CalculateTime;
import com.scbo.utility.InputNumberFilter;
import com.scbo.utility.ItemCreateArrayAdapter;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.InputFilter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class CreateItemActivity extends Activity implements OnClickListener {
	Buildorder buildOrder;
	AlertDialog.Builder addDialogBuilder;
	EditText etTimeMinute;
	EditText etTimeSecond;
	EditText etText;
	EditText etTextToSpeech;
	ListView itemsListView;
	ArrayList<ItemBO> listItems = null;
	ItemCreateArrayAdapter adapter = null;
	private ScboDAO datasource;
	int checkBO = 0;
	int checkItem = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_create_item);
		buildOrder = (Buildorder) getIntent().getSerializableExtra(
				"buildOrder");
		itemsListView = (ListView) findViewById(R.id.lvItemCreate);
		listItems = new ArrayList<ItemBO>();

		adapter = new ItemCreateArrayAdapter(this, listItems);
		itemsListView.setAdapter(adapter);

		datasource = new ScboDAO(this);
		datasource.open();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.create_item, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		Intent i = null;
		switch (v.getId()) {
		case R.id.btnAddItem:

			LayoutInflater layoutInflater = LayoutInflater.from(this);
			View addMenuView = layoutInflater.inflate(R.layout.menu_item, null);

			etTimeMinute = (EditText) addMenuView
					.findViewById(R.id.etInputTimeMinute);
			etTimeMinute.setFilters(new InputFilter[] { new InputNumberFilter(
					"0", "60") });
			etTimeSecond = (EditText) addMenuView
					.findViewById(R.id.etInputTimeSecond);
			etTimeSecond.setFilters(new InputFilter[] { new InputNumberFilter(
					"0", "60") });
			etText = (EditText) addMenuView.findViewById(R.id.etInputText);
			etTextToSpeech = (EditText) addMenuView
					.findViewById(R.id.etInputTextTSS);

			addDialogBuilder = new AlertDialog.Builder(this);
			addDialogBuilder.setView(addMenuView);
			addDialogBuilder
					.setCancelable(false)
					.setPositiveButton("Cancel",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									dialog.cancel();
								}
							})
					.setCancelable(false)
					.setNegativeButton("Add",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									String minute = etTimeMinute.getText()
											.toString().trim();
									String second = etTimeSecond.getText()
											.toString().trim();
									long milisecond = CalculateTime
											.convertToMilisecond(minute, second);
									String text = etText.getText().toString()
											.trim();
									String textToSpeech = etTextToSpeech
											.getText().toString().trim();
									ItemBO item = new ItemBO(text, milisecond,
											textToSpeech);
									Log.d("KHANHVQ",
											"milisecond = " + item.getTime()
													+ " text ="
													+ item.getText() + " TTS="
													+ item.getTextToSpeech());
									listItems.add(item);
									adapter.notifyDataSetChanged();
									// dialog.cancel();
								}
							});
			AlertDialog addDialog = addDialogBuilder.create();
			addDialog.show();

			break;
		case R.id.btnSaveBuilds:
			if (checkBO == 0 && checkItem == 0) {
				checkBO = datasource.createBuildOrder(buildOrder);
				for (int j = 0; j < listItems.size(); j++) {
					ItemBO item = new ItemBO();
					item = listItems.get(j);
					buildOrder.setId(checkBO);
					item.setBuildOrder(buildOrder);
					checkItem = datasource.createItemBO(item);
				}
				Log.d("KHANHVQ", "CHECK DATASOURCE: idBO = " + checkBO
						+ " idItem =" + checkItem);
				Toast.makeText(this, "Build Order is saved", Toast.LENGTH_LONG)
						.show();
			} else {
				Toast.makeText(this, "Build Order was already existed",
						Toast.LENGTH_LONG).show();
			}

			break;
		case R.id.btnUpload:

			break;

		default:
			break;
		}
	}

	@Override
	protected void onResume() {
		datasource.open();
		super.onResume();
	}

	@Override
	protected void onPause() {
		datasource.close();
		super.onPause();
	}
}
